﻿#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickStyle>
#include <QQmlContext>

#include "common/__defines.h"
#include "common/frameless.h"
#include "common/dbcontrol.h"
#include "common/xlsxoperate.h"
#include "modbushandler.h"

int main(int argc, char *argv[])
{
    qRegisterMetaType<OperatePoint>("OperatePoint");

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QCoreApplication::setOrganizationName("Highlander");
    QGuiApplication app(argc, argv);
    QQuickStyle::setStyle("Material");

    QQmlApplicationEngine engine;
    DBControl* m_dbControl = new DBControl();

    qmlRegisterType<FramelessWindow>("Qt.Window", 1, 0, "Frameless");
    qmlRegisterSingletonType(QUrl("qrc:/_context/Basic/SkinModel.qml"), "Qt.Singleton", 1, 0, "SkinSingleton");

    qmlRegisterType<ModbusHandler>("ModbusHandler", 1, 0, "ModbusHandler");

    engine.rootContext()->setContextProperty("DBControl", m_dbControl);

    XlsxOperate* xlsx = new XlsxOperate();
    auto context = engine.rootContext();
    context->setContextProperty("XLSX", xlsx);

    const QUrl url(QStringLiteral("qrc:/_context/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}
